<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home_controller extends public_controller
{
//	public function __construct()
//	{
//		parent::__construct();
//		$this->addCss('home');
//	}

	public function index()
    {

        $tables = $this->table_model->get_elements();
        $this->view_data['tables_html'] = $this->load->view('tables',array('tables'=>$tables),TRUE);
        $this->view_data['products'] = $this->products_model->get_elements();
        $this->load_view('index');
    }

    public function window_print($order_id)
    {
        $order = $this->order_model->get_element_by_id($order_id);
        $order_detail = $this->order_detail_model->get_elements_by_order_id($order_id);
        $data = array(
            'order' => $order,
            'order_detail' => $order_detail,
            'information' => $this->information_model->get_first_element()
        );
        $this->load->view('window_print',$data);
    }

    public function add_order()
    {
        $table_id = $this->input->post('table_id');
        $result = array();
        $result['status'] = false;
        if($this->store_model->get_element_by_table($table_id)->id != '')
        {
            $cart = $this->store_model->get_elements_by_table($table_id);
            $total = 0;
            foreach($cart as $item)
            {
                $total += $item->sub_total;
            }

            $data = array(
                'table_id' => $table_id,
                'created_date' => time(),
                'total' => $total
            );

            if($this->order_model->add_order($data))
            {
                $districtid = $this->db->insert_id();
                $result['order_id'] = $districtid;
                foreach($cart as $item)
                {
                    $data = array(
                        'order_id' => $districtid,
                        'product_id' => $item->product_id,
                        'quantity' => $item->quantity,
                        'sub_total' => $item->sub_total
                    );
                    $result['status'] = $this->order_detail_model->add_order_detail($data);
                }
            }


        }


        echo json_encode($result);
    }

    public function get_table()
    {
        $data = array();
        $id = $this->input->post('id');
        $table_item = $this->table_model->get_element_by_id($id);
        $data['table_item'] = $table_item;
        $html = $this->load->view('popup_form',$data,true);
        echo json_encode($html);
    }

    public function search_product()
    {
        $keyword = $this->input->post('content');
        $result = $this->products_model->search_product($keyword);
        echo json_encode($result);
    }

    public function add_product_to_table()
    {
        $result = FALSE;
        $table_id = $this->input->post('table_id');
        $product_id = $this->input->post('product_id');
        $quantity = $this->input->post('quantity');
        $product_item = $this->store_model->get_element_by_table_product($table_id,$product_id);

        if($product_item->id == '')
        {
            $data = array(
                'table_id' => $table_id,
                'product_id' => $product_id,
                'quantity' => $quantity
            );
            if($this->store_model->add_product($data))
                $result = TRUE;
        }
        else
        {
            $data = array('quantity' => $quantity);
            if($this->store_model->increase_quantity($product_item->id,$data))
                $result = TRUE;
        }
        echo json_encode($result);
    }

    public function get_all_store()
    {
        $table_id = $this->input->post('table_id');
        $result = $this->store_model->get_elements_by_table($table_id);
        echo json_encode($result);
    }

    public function clear_all()
    {
        $table_id = $this->input->post('table_id');
        $result = $this->store_model->delete_by_table($table_id);
        echo json_encode($result);
    }

    public function reload_table_list()
    {
        $tables = $this->table_model->get_elements();
        $html = $this->load->view('tables',array('tables' => $tables),TRUE);
        echo json_encode($html);
    }


    public function increase_quantity()
    {
        $result = false;
        $table_id = $this->input->post('table_id');
        $product_id = $this->input->post('product_id');
        $quantity = $this->input->post('quantity');
        $product_item = $this->store_model->get_element_by_table_product($table_id,$product_id);
        $data = array('quantity' => $quantity + $product_item->quantity);
        if($this->store_model->increase_quantity($product_item->id,$data))
            $result = true;;
        echo json_encode($result);
    }

    public function decrease_quantity()
    {
        $result = false;
        $table_id = $this->input->post('table_id');
        $product_id = $this->input->post('product_id');
        $quantity = $this->input->post('quantity');
        $product_item = $this->store_model->get_element_by_table_product($table_id,$product_id);
        $data = array('quantity' => $quantity - $product_item->quantity);
        if($this->store_model->increase_quantity($product_item->id,$data))
            $result = true;;
        echo json_encode($result);
    }

}
